package com.ruoyi.zcxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zcxt.domain.ZcUse;
import com.ruoyi.zcxt.service.IZcUseService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 资产用途（树）Controller
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@RestController
@RequestMapping("/zcxt/use")
public class ZcUseController extends BaseController
{
    @Autowired
    private IZcUseService zcUseService;

    /**
     * 查询资产用途（树）列表
     */
    @PreAuthorize("@ss.hasPermi('zcxt:use:list')")
    @GetMapping("/list")
    public AjaxResult list(ZcUse zcUse)
    {
        List<ZcUse> list = zcUseService.selectZcUseList(zcUse);
        return success(list);
    }

    /**
     * 导出资产用途（树）列表
     */
    @PreAuthorize("@ss.hasPermi('zcxt:use:export')")
    @Log(title = "资产用途（树）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcUse zcUse)
    {
        List<ZcUse> list = zcUseService.selectZcUseList(zcUse);
        ExcelUtil<ZcUse> util = new ExcelUtil<ZcUse>(ZcUse.class);
        util.exportExcel(response, list, "资产用途（树）数据");
    }

    /**
     * 获取资产用途（树）详细信息
     */
    @PreAuthorize("@ss.hasPermi('zcxt:use:query')")
    @GetMapping(value = "/{useId}")
    public AjaxResult getInfo(@PathVariable("useId") Long useId)
    {
        return success(zcUseService.selectZcUseByUseId(useId));
    }

    /**
     * 新增资产用途（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:use:add')")
    @Log(title = "资产用途（树）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ZcUse zcUse)
    {
        zcUse.setCreateBy(getUsername());
        return toAjax(zcUseService.insertZcUse(zcUse));
    }

    /**
     * 修改资产用途（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:use:edit')")
    @Log(title = "资产用途（树）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcUse zcUse)
    {
        Long useId = zcUse.getUseId();
        if (zcUse.getParentId().equals(useId)) {
            return error("修改用途'" + zcUse.getUseName() + "'失败，上级分类不能是自己");
        }else if (StringUtils.equals(ZcxtConstants.USE_DISABLE, zcUse.getStatus()) && zcUseService.selectNormalChildrenZcUseByUseId(useId) > 0){
            return error("该分类包含未停用的子分类");
        }
        zcUse.setUpdateBy(getUsername());
        return toAjax(zcUseService.updateZcUse(zcUse));
    }

    /**
     * 删除资产用途（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:use:remove')")
    @Log(title = "资产用途（树）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{useId}")
    public AjaxResult remove(@PathVariable Long useId)
    {
        if (zcUseService.hasChildByUseId(useId)){
            return warn("存在下级分类，不允许删除");
        }
        return toAjax(zcUseService.deleteZcUseByUseIds(useId));
    }
}
